package com.qr.controller.wmyh;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.RestController;
import cn.krui.helper.bean.AppResult;
import cn.krui.helper.bean.DataArray;
import com.qr.controller.WmyhBaseController;
import com.qr.bean.wmyh.JbrUserGrade;
import com.qr.bean.wmyh.JbrUserGradeView;
import com.qr.service.wmyh.JbrUserGradeService;
import java.util.List;
import javax.validation.Valid;
import org.springframework.validation.BindingResult;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.validation.ObjectError;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import cn.krui.helper.tools.DateTimeUtils;
/**
 * @author: zc
 * email: zc8023zs@163.com
 * create_time: 2020/8/11 13:03:16
 */
@Api(tags = "jbr_user_grade", description = "会员等级")
@RestController
@RequestMapping("/wmyh/jbr_user_grade")
public class JbrUserGradeController extends WmyhBaseController {

    @Autowired
    private JbrUserGradeService jbrUserGradeService;

    @PostMapping(value = "/add")
	@ApiOperation(value = "会员等级添加", httpMethod = "POST")
    public AppResult<Integer> add(@Valid JbrUserGrade model,BindingResult bindingResult){
        AppResult<Integer> app = new AppResult<Integer>();
        if (bindingResult.hasErrors()) {
            List<ObjectError> oerr = bindingResult.getAllErrors();
            for (ObjectError objectError : oerr) {
                DefaultMessageSourceResolvable d = (DefaultMessageSourceResolvable)objectError.getArguments()[0];
                String columnname = d.getDefaultMessage();
                //主键不用判断
                if("id".equals(columnname)){
					continue;
				}
                app.no(bindingResult.getAllErrors().get(0).getDefaultMessage());
                return app;
            }
        }
        Integer i = jbrUserGradeService.add(model);
        if(i>0){
			i = model.getId();
		}
        app.setDataSingle(i);
        app.ok("添加成功");
        return app;
    }

    @PostMapping(value = "/update")
	@ApiOperation(value = "会员等级修改", httpMethod = "POST")
    public AppResult<Integer> update(@Valid JbrUserGrade model,BindingResult bindingResult){
        AppResult<Integer> app = new AppResult<Integer>();
        if (bindingResult.hasErrors()) {
            List<ObjectError> oerr = bindingResult.getAllErrors();
            for (ObjectError objectError : oerr) {
                DefaultMessageSourceResolvable d = (DefaultMessageSourceResolvable)objectError.getArguments()[0];
                String columnname = d.getDefaultMessage();
                app.no(bindingResult.getAllErrors().get(0).getDefaultMessage());
                return app;
            }
        }
        Integer i = jbrUserGradeService.update(model);
        app.ok("修改成功");
        app.setDataSingle(i);
        return app;
    }

    @PostMapping(value = "/del")
	@ApiOperation(value = "会员等级删除", httpMethod = "POST")
    public AppResult<Integer> del(Integer id){
        AppResult<Integer> app = new AppResult<Integer>();
        Integer i = jbrUserGradeService.del(id);
        app.ok("删除成功");
        app.setDataSingle(i);
        return app;
    }

	/**
	 * @JSON(type =JbrUserGradeView.class, include = "") //只支持简单对象序列化过滤，自行添加需要输出的字段
	 */
    @GetMapping(value = "/info")
	@ApiOperation(value = "会员等级详情", httpMethod = "GET")
    public AppResult<JbrUserGradeView> info(Integer id){
        AppResult<JbrUserGradeView> app = new AppResult<JbrUserGradeView>();
        JbrUserGradeView i = jbrUserGradeService.info(id);
        app.setDataSingle(i);
        app.ok("查询成功");
        return app;
    }

	/**
	 * @JSON(type =JbrUserGradeView.class, include = "") //只支持简单对象序列化过滤，自行添加需要输出的字段
	 */
    @PostMapping(value = "/select")
	@ApiOperation(value = "会员等级列表", httpMethod = "POST") 
    public AppResult<JbrUserGradeView> select(
        ){
        AppResult<JbrUserGradeView> app = new AppResult<JbrUserGradeView>();
        JbrUserGradeView where = new JbrUserGradeView();
        List<JbrUserGradeView> i = jbrUserGradeService.select(where);
        DataArray<JbrUserGradeView> da=new DataArray<JbrUserGradeView>();
        da.setDataList(i);
        app.setDataArray(da);
        app.ok("查询成功");
        return app;
    }

	/**
	 * @JSON(type =JbrUserGradeView.class, include = "") //只支持简单对象序列化过滤，自行添加需要输出的字段
	 */
    @PostMapping(value = "/selectpage")
    @ApiOperation(value = "会员等级列表分页", httpMethod = "POST")
    public AppResult<JbrUserGradeView> selectpage(
            @ApiParam(value = "页", required = true) @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
            @ApiParam(value = "条", required = true) @RequestParam(value = "size", required = false, defaultValue = "20") Integer size) {
        AppResult<JbrUserGradeView> app = new AppResult<JbrUserGradeView>();
        JbrUserGradeView where = new JbrUserGradeView();
        IPage<JbrUserGradeView> pg = new Page<JbrUserGradeView>();
        pg.setCurrent(page); 
        pg.setSize(size);
        IPage<JbrUserGradeView> i = jbrUserGradeService.select(pg,where);
        int total = jbrUserGradeService.selectCount(where);
        DataArray<JbrUserGradeView> da = new DataArray<JbrUserGradeView>();
        da.setPageCount((total/size)+1);
        da.setDataCount(total);
        da.setPageIndex(page);
        da.setPageSize(size);
        da.setDataList(i.getRecords());
        app.setDataArray(da);
        app.ok("查询成功");
        return app;
    }

}